package com.gress.shop.dao.impl;

import com.gress.shop.bo.MemberActionRelation;
import com.gress.shop.bo.example.MemberActionRelationExample;
import com.gress.shop.dao.IMemberActionRelationDao;
import com.gress.shop.mapper.MemberActionRelationMapper;
import com.gress.shop.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created xiongwen with 2018/3/6 15:44
 * Description:
 */
@Service
public class MemberActionRelationDaoImpl implements IMemberActionRelationDao {

    @Resource
    private MemberActionRelationMapper relationMapper;

    @Override
    public int deleteByExample(MemberActionRelationExample example) {
        return relationMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return relationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByActionId(String actionId) {
        return relationMapper.deleteByActionId(actionId);
    }

    @Override
    public List<MemberActionRelation> selectByExample(MemberActionRelationExample example) {
        return relationMapper.selectByExample(example);
    }

    @Override
    public MemberActionRelation selectByPrimaryKey(String id) {
        return relationMapper.selectByPrimaryKey(id);
    }

    @Override
    public MemberActionRelation insertOrUpdate(MemberActionRelation memberActionRelation) {
        if (StringUtils.isEmpty(memberActionRelation.getId())) {
            memberActionRelation.setId(UUIDUtil.getUUID());
            relationMapper.insertSelective(memberActionRelation);
        }else {
            relationMapper.updateByPrimaryKeySelective(memberActionRelation);
        }
        return memberActionRelation;
    }

    @Override
    public MemberActionRelation queryMemberIdAndActionId(String memberId, String actionId) {
        MemberActionRelationExample example = new MemberActionRelationExample();
        example.createCriteria()
                .andMemberIdEqualTo(memberId)
                .andActionIdEqualTo(actionId)
                .andIsValidEqualTo(1);
        List<MemberActionRelation> relationList = relationMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(relationList)) {
            return null;
        }
        return relationList.get(0);
    }

    @Override
    public List<MemberActionRelation> selectByMemberId(String memberId) {
        MemberActionRelationExample example = new MemberActionRelationExample();
        example.createCriteria()
                .andMemberIdEqualTo(memberId)
                .andIsValidEqualTo(1);
        return relationMapper.selectByExample(example);
    }
}
